/*
 * Copyright 2011 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.abcsolutions.g5.base.event;

import java.util.Vector;

/**
 * @author Michael Nitsche
 * 02.03.2011	Converted to G5
 */
public abstract class A_HasListeners {

	private final Vector<Enum<?>> keys;
	private final Vector<I_Listener<?>> listeners;


	public A_HasListeners() {
		this.keys= new Vector<Enum<?>>();
		this.listeners= new Vector<I_Listener<?>>();
	}


	protected void listenerAdd(Enum<?> ident, I_Listener<?> l) {
		if(ident==null || l==null)
			throw new NullPointerException();
		this.keys.add(ident);
		this.listeners.add(l);
	}

	protected void listenerStart(Enum<?> ident) {
		listenerStart(ident, null);
	}

	@SuppressWarnings("unchecked")
	protected <T1> void listenerStart(Enum<?> ident, T1 data) {
		if(ident==null)
			throw new NullPointerException();
		for(int i=0; i<this.keys.size(); i++) {
			if(this.keys.get(i).equals(ident)) {
				I_Listener<T1> l2=(I_Listener<T1>) this.listeners.get(i); //Throws Exception at wrong types
				l2.handle(data);
			}
		}
	}

}
